package com.samphanie.auiu.router.utils;

import android.content.ComponentName;
import android.content.Context;
import android.content.res.AssetManager;
import android.view.Menu;
import android.view.MenuItem;

import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.navigation.ActivityNavigator;
import androidx.navigation.NavController;
import androidx.navigation.NavGraph;
import androidx.navigation.NavGraphNavigator;
import androidx.navigation.NavigatorProvider;
import androidx.navigation.fragment.DialogFragmentNavigator;
import androidx.navigation.fragment.FragmentNavigator;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.samphanie.auiu.router.MainActivity;
import com.samphanie.auiu.router.R;
import com.samphanie.auiu.router.model.BottomBar;
import com.samphanie.auiu.router.model.Destination;

import org.jetbrains.annotations.NotNull;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

/**
 * @author ZSY
 */
public class NavUtil {

    // private static int[] sIcons = new int[]{R.drawable.icon_tab_home, R.drawable.icon_tab_sofa, R.drawable.icon_tab_publish, R.drawable.icon_tab_find, R.drawable.icon_tab_mine};

    private static HashMap<String, Destination> destinations;

    public static String parseFile(Context context, String filename) {
        AssetManager assets = context.getAssets();

        try {
            InputStream inputStream = assets.open(filename);
            String line = null;
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            StringBuilder builder = new StringBuilder();

            while ((line = bufferedReader.readLine()) != null) {
                builder.append(line);
            }

            inputStream.close();
            bufferedReader.close();

            return builder.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static void builderNavGraph(FragmentActivity activity, FragmentManager childFragmentManager, NavController controller, int containerId) {
        String content = parseFile(activity, "destination.json");

        destinations = JSON.parseObject(content, new TypeReference<HashMap<String, Destination>>() {
        }.getType());

        NavigatorProvider provider = controller.getNavigatorProvider();
        NavGraphNavigator graphNavigator = provider.getNavigator(NavGraphNavigator.class);
        NavGraph navGraph = new NavGraph(graphNavigator);

        AuiuFragmentNavigator auiuFragmentNavigator = new AuiuFragmentNavigator(activity, childFragmentManager, containerId);
        provider.addNavigator(auiuFragmentNavigator);
        Iterator<Destination> iterator = destinations.values().iterator();
        while (iterator.hasNext()) {
            Destination destination = iterator.next();
            if (destination.getDestType().equals("activity")) {
                ActivityNavigator navigator = provider.getNavigator(ActivityNavigator.class);
                ActivityNavigator.Destination node = navigator.createDestination();

                node.setId(destination.getId());
                node.setComponentName(new ComponentName(activity.getPackageName(), destination.getClazName()));

                navGraph.addDestination(node);
            }

            if (destination.getDestType().equals("fragment")) {
//                FragmentNavigator navigator = provider.getNavigator(FragmentNavigator.class);
//                FragmentNavigator.Destination node = navigator.createDestination();

                AuiuFragmentNavigator.Destination node = auiuFragmentNavigator.createDestination();

                node.setId(destination.getId());
                node.setClassName(destination.getClazName());

                navGraph.addDestination(node);
            }

            if (destination.getDestType().equals("dailog")) {
                DialogFragmentNavigator navigator = provider.getNavigator(DialogFragmentNavigator.class);
                DialogFragmentNavigator.Destination node = navigator.createDestination();

                node.setId(destination.getId());
                node.setClassName(destination.getClazName());

                navGraph.addDestination(node);
            }

            if (destination.isAsStarter()) {
                navGraph.setStartDestination(destination.getId());
            }
        }

        controller.setGraph(navGraph);
    }

    public static void builderBottomBar(BottomNavigationView navView) {
        String content = parseFile(navView.getContext(), "main_tabs_config.json");

        BottomBar bottomBar = JSON.parseObject(content, BottomBar.class);
        List<BottomBar.Tab> tabs = bottomBar.getTabs();
        Menu menu = navView.getMenu();
        for (BottomBar.Tab tab : tabs) {
            if (!tab.isEnable()) continue;

            Destination destination = destinations.get(tab.getPageUrl());
            if (destination != null) {
                MenuItem menuItem = menu.add(0, destination.getId(), tab.getIndex(), tab.getTitle());
                // menuItem.setIcon(sIcons[tab.getIndex()]);
                menuItem.setIcon(R.drawable.ic_home_black_24dp);
            }
        }

    }

}
